package com.gqz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gqz.domain.BlogArticle;
import com.gqz.domain.SysUser;
import com.gqz.dto.ArticleDetailDto;
import com.gqz.dto.ArticleGdDto;
import com.gqz.dto.RelatedArticleDto;
import com.gqz.mapper.BlogArticleMapper;
import com.gqz.query.BaseQuery;
import com.gqz.service.IBlogArticleService;
import com.gqz.utils.PageList;
import com.gqz.utils.UserTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BlogArticleServiceImpl implements IBlogArticleService {


    @Autowired
    private BlogArticleMapper blogArticleMapper;

    @Override
    public PageList<BlogArticle> getAllArticle(BaseQuery query) {

        // 1、使用分页插件分页
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        // 2、开始查询
        List<BlogArticle> blogArticles = blogArticleMapper.getAllArticle(query);
        // 3、使用分页工具类封装数据
        PageInfo<BlogArticle> pageInfo = new PageInfo<>(blogArticles);
        // 4、 使用自定义的分页工具类封装前端需要的数据
        PageList<BlogArticle> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        return pageList;
    }

    @Override
    public boolean insertArticle(BlogArticle article, HttpServletRequest request) {
        SysUser userInfo = UserTools.getUserInfo(request);
        article.setUserId(userInfo.getId());
        return blogArticleMapper.insert(article) > 0;
    }

    @Override
    public boolean updateArticle(BlogArticle article) {
        return blogArticleMapper.updateByPrimaryKey(article) > 0;
    }

    @Override
    public boolean batchDeleteArticle(Long[] ids) {
        return blogArticleMapper.batchDeleteArticle(ids) > 0;
    }

    @Override
    public boolean deleteArticle(Long id) {
        return blogArticleMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public BlogArticle getArticleById(Long id) {
        return blogArticleMapper.selectByPrimaryKey(id);
    }

    @Override
    public ArticleDetailDto getArticleDetailById(Long id) {
        return blogArticleMapper.getArticleDetailById(id);
    }

    @Override
    public ArticleGdDto getBlogGdList(String time) {
        //如果等于-1表示查询所有，否则按照对应的年月查询文章信息
        time = "all".equalsIgnoreCase(time) ? "" : time;
        List<BlogArticle> list = blogArticleMapper.getBlogGdList(time);
        //根据年份进行分组
        Map<String, List<BlogArticle>> resultMap = list.stream().collect(Collectors.groupingBy(BlogArticle::getYear));
        List<ArticleGdDto> gdList = new ArrayList<>();
        ArticleGdDto gd = null;
        //遍历resultMap，封装结果进行返回
        for (Map.Entry<String, List<BlogArticle>> entry : resultMap.entrySet()) {
            gd = new ArticleGdDto();
            gd.setYear(entry.getKey());
            gd.setArticleList(entry.getValue());
            gdList.add(gd);
        }
        return gd;
    }

    @Override
    public Object selectArticleTimeStatistics() {
        return null;
    }

    @Override
    public List<RelatedArticleDto> getRelatedArticle(Long id) {
        return blogArticleMapper.getRelatedArticle(id);
    }
}
